﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi.AppAuth
{
    [CPPEI.WebApi.AuthPostAction("AppAuth", "Create", "1.0")]
    public class CreateAction : BaseAction<CreateParams, CreateResult>
    {
        protected override ActionResult<CreateResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            CreateParams opParams)
        {
            SER.AppAuthorizationService service = new SER.AppAuthorizationService();
            ServiceResult<ENT.EAppAuthorization> serResult = service.Create(context.UserId, opParams.Name, opParams.Description);
            if(serResult.IsSuccess)
            {
                CreateResult result = new CreateResult();
                result.AppId = serResult.Data.AppId;
                result.Id = serResult.Data.Id;
                result.Token = serResult.Data.Token;

                return new ActionResult<CreateResult>(result);
            }
            else
            {
                return new ActionResult<CreateResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class CreateParams
    {
        [Parameter]
        public string Name { get; set; }

        [Parameter]
        public string Description { get; set; }
    }

    public class CreateResult
    {
        public Guid Id { get; set; }

        public Guid AppId { get; set; }

        public string Token { get; set; }
    }
}
